fix: handle tz-aware datetimes in naturalday and naturaldate#297
Merged
hugovk merged 1 commit intopython-humanize:mainfrom Feb 22, 2026
Merged
fix: handle tz-aware datetimes in naturalday and naturaldate#297hugovk merged 1 commit intopython-humanize:mainfrom
hugovk merged 1 commit intopython-humanize:mainfrom
Conversation
When a tz-aware datetime is passed to naturalday() or naturaldate(), the date comparison should use 'today' in the datetime's timezone rather than the system's local date. Previously the timezone was stripped and the date compared against dt.date.today(), which could yield wrong results (e.g. returning 'tomorrow' when the local date in the given timezone is actually the same day). Fixes python-humanize#152
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #297 +/- ##
==========================================
+ Coverage 99.51% 99.52% +0.01%
==========================================
Files 11 11
Lines 831 851 +20
==========================================
+ Hits 827 847 +20
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
Please see also similar PR #296, review it, and say which you think is better. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #152
When a tz-aware datetime is passed to
naturalday()ornaturaldate(), the functions now compute "today" in the datetime's timezone instead of using the system's local date.For example, if the current UTC time is
2023-10-15 23:00and you pass a datetime in AEDT (+11:00) where the local date is already Oct 16th, the comparison should use Oct 16th as "today" for that timezone. Previously the timezone was stripped and compared against the system local date, which gave wrong results when the tz-local date differed from the system date.The fix checks whether the value is a tz-aware datetime and, if so, calls
datetime.now(value.tzinfo).date()instead ofdate.today(). The existing behavior for naive datetimes and plaindateobjects is unchanged.Test included using the exact scenarios from the issue.